using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Diagnostics;
using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic.BehaviorTree;
using Styx.Logic.Combat;
using Styx.Logic.Inventory;
using Styx.Patchables;
using Styx.Plugins.PluginClass;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace HBPVPDruidMovement
{
    class HBPVPDruidMovement : HBPlugin
    {

        #region override

        public override void Initialize()
        {
        }


        public override bool WantButton
        {
            get
            {
                return true;
            }
        }

        public override string ButtonText
        {
            get
            {
                return "Settings";
            }
        }

        public override string Author 
        {
            get
            {
                return "SwInY";
            }
        }

        public override string Name
        {
            get
            {
                return "HBPVPDruidMovement";
            }
        }

        public override Version Version
        {
            get
            {
                return new Version(1, 0, 0, 0);
            }
        }

        #endregion



        private static LocalPlayer Me { get { return ObjectManager.Me; } }
        private static bool JumpLeft = false;   // Jumpleft = true; Jumpleft = false = right
        private static bool RandomJump = true; // Random jump every now and then
        private static Stopwatch StrafeTimer = new Stopwatch(); // We use a timer instead of thread.sleep so we can still do stuff, I.E Cast

        public override void Pulse()
        {
            // Error Checking
            if (StyxWoW.Me == null || !StyxWoW.IsInGame || !StyxWoW.IsInWorld) return;
            WoWUnit Targeting = Me.CurrentTarget;
            if (Targeting == null) return;
            if (Targeting.Distance > 40) return;
            if (!Targeting.IsAlive) return;
            if (!Me.IsActuallyInCombat) return;

            if (!Me.HasAura("Eclipse(Solar)"))
            {
                if (!Targeting.HasAura("Insect Swarm") || !Targeting.HasAura("Moonfire")) return;
            }

            // So we can still cast while we move
            if (CheckStrafeTimer)
            {
                StrafeTimer.Stop();
                StrafeTimer.Reset();
                WoWMovement.MoveStop();
            }

           

            // Add more than strafe later
            DoRandomStrafe();

        }



        // Do facy movements.
        // coming soon.
        // just coding main core first
        public static void DoRandomStrafe()
        {

            // Get which way to strafe,
            WoWMovement.MovementDirection WayToStrafe;  // Create Var
            WayToStrafe = WoWMovement.MovementDirection.StrafeRight; // Set to default right
            if (JumpLeft == true) WayToStrafe = WoWMovement.MovementDirection.StrafeLeft; // if left set to left

            // Ok now lets strafe, If all good throw in a jump if we want to
            WoWMovement.Move(WayToStrafe);
            DoRandomJump();
            StrafeTimer.Start();

            if (WayToStrafe == WoWMovement.MovementDirection.StrafeLeft) DebugLog("[Movement] Strafing Left");
            if (WayToStrafe == WoWMovement.MovementDirection.StrafeRight) DebugLog("[Movement] Strafing Right");

            // One lined this shit so looks neater for now.
            if (JumpLeft == true) { JumpLeft = false; } else { JumpLeft = true; }
        }

        /// <summary>
        /// True if over the timer limit
        /// </summary>
        private static bool CheckStrafeTimer
        {
            get
            {
                DebugLog("StrafeTimer: MS[{0}]", StrafeTimer.ElapsedMilliseconds);
                if (StrafeTimer.ElapsedMilliseconds > 400) return true;
                return false;
            }
        }

        /// <summary>
        /// will 50/50 Randomly jump
        /// </summary>
        private static void DoRandomJump()
        {
            if (ShouldIRandomJump)
            {
                DebugLog("[Movement]: Randomly Jumping");
                WoWMovement.Move(WoWMovement.MovementDirection.JumpAscend);
                StyxWoW.SleepForLagDuration();
            }
        }

        /// <summary>
        /// Determines if it should jump or not
        /// </summary>
        private static bool ShouldIRandomJump
        {
            get
            {
                // 50/50 change of jumping seems good.
                Random num = new Random();
                int numb = num.Next(0, 3);

                if (numb == 1) return true;
                return false;
            }
        }


        public static void DebugLog(string format, params object[] args)
        {
            Logging.Write(System.Drawing.Color.Blue, "[PVPDruidMovement By SwInY] " + string.Format(format, args));
            Logging.WriteDebug(System.Drawing.Color.Blue, "[PVPDruidMovement By SwInY] " + string.Format(format, args));
        }
    }
}
